#include "realdatarightwidget.h"
#include "ui_realdatarightwidget.h"
#include "widget/Topology/zbnnmw.h"

#include <QGridLayout>
#include <QDebug>

RealDataRightWidget::RealDataRightWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RealDataRightWidget),
    isOne(true)
{
    ui->setupUi(this);
    ui->stackedWidget->removeWidget(ui->stackedWidget->widget(0));
    ui->stackedWidget->removeWidget(ui->stackedWidget->widget(0));
    initUI();
    connect(topologyMap,SIGNAL(sig_tree_clear()),this,SIGNAL(sig_tree_clear()));
}

RealDataRightWidget::~RealDataRightWidget()
{
    delete ui;
}
void RealDataRightWidget::initUI()
{

    QStringList headNames;
    //日期 时间 设备名 描述 值

    headNames<<trUtf8("日期")<<trUtf8("时间")<<trUtf8("设备名")<<trUtf8("描述")<<trUtf8("值");

    listWidget = new TableWidget(headNames,0);
    topologyMap = new ZBNNMW;//QWidget;
    lineGraph = new LineGraphWidget(-20,60,0);
    setArguments = new SetArgumentsWidget;


    ui->stackedWidget->addWidget(listWidget);
    ui->stackedWidget->addWidget(topologyMap);
    ui->stackedWidget->addWidget(lineGraph);
    ui->stackedWidget->addWidget(setArguments);

    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->addWidget(ui->pushButton,1,0,1,1);
    mainLayout->addWidget(ui->pushButton_2,1,2,1,1);
    mainLayout->addWidget(ui->pushButton_3,1,4,1,1);
    mainLayout->addWidget(ui->stackedWidget,2,0,8,5);

    setLayout(mainLayout);

    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(showListWidget()));
    connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(doChartButton()));
    connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(showSetArguments()));

    connect(setArguments,SIGNAL(sig_Afilter(Filter)),this,SIGNAL(sig_aFilter(Filter)));

}
void RealDataRightWidget::showListWidget()//列表窗口
{
    ui->stackedWidget->setCurrentIndex(0);
}
void RealDataRightWidget::showTopologyMap()//网络拓扑
{   
    ui->stackedWidget->setCurrentIndex(1);
}
void RealDataRightWidget::showLineGraph()//折线图
{
    ui->stackedWidget->setCurrentIndex(2);//进行测试可以将参数改为1
}
void RealDataRightWidget::showSetArguments()//参数窗口
{
    ui->stackedWidget->setCurrentIndex(3);
}
void RealDataRightWidget::clearListWidget()
{
    listWidget->clearAllItems();
}
void RealDataRightWidget::setListWidgetItems(const QList<QTableWidgetItem *> &list)
{
    listWidget->addItems(list);
}
void RealDataRightWidget::doChartButton()
{
    if (isOne) {
        showLineGraph();
    }
    else {
        showTopologyMap();
    }
}
void RealDataRightWidget::slt_lineGrapShowData(const QString& adress, int value)
{
    lineGraph->showData(adress,value);
}


void RealDataRightWidget::slt_netTopologyBuild(const QString &value)
{
    topologyMap->slt_netTopologyBuild(value);
}

void RealDataRightWidget::slt_showAllorOne(const QString &value)
{
    if (value == "ALLNODE") {
        isOne = false;
        if (ui->stackedWidget->currentIndex() == 2) {
            ui->stackedWidget->setCurrentIndex(1);
        }

    } else {
        if (ui->stackedWidget->currentIndex() == 1) {
            ui->stackedWidget->setCurrentIndex(2);
        }
        isOne = true;
    }
}

void RealDataRightWidget::slt_newNode(const Node &n)
{
    topologyMap->AddNodeToMap(n);
}
//void RealDataRightWidget::slt_oldNode(const Node &n)
//{
//    topologyMap->slt_oldNode(n);
//}
//void RealDataRightWidget::slt_statesChange(ushort name, bool states)
//{
//    topologyMap->slt_statesChange(name,states);
//}
void RealDataRightWidget::slt_lineGrapShowData(ushort name, int value)
{
    lineGraph->slt_showData(name,value);
}
void RealDataRightWidget::slt_ChangNodeState(const Node &n)
{
    topologyMap->ChangNodeState(n);
}

void RealDataRightWidget::slt_ChangNodeState(int type,const QString &addr,
                                             const QString &paddr,
                                             ushort _name,bool status)
{
    topologyMap->ChangNodeState(type,addr, paddr, _name,status);
}
void RealDataRightWidget::slt_updataNodeValue(ushort name, int value)
{
     this->topologyMap->slt_updataNodeValue(name,value);
}

